﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eCommerce.Core;
using eCommerce.Core.Domain.ShoppingCarts;
using eCommerce.Services.Pictures;
using eCommerce.Services.Products;
using eCommerce.Services.ShoppingCarts;

namespace eCommerce.Controllers
{
    public class ShoppingCartController : Controller
    {
        private IShoppingCartService _shoppingCartService;
        private IWorkContext _workContext;
        private IProductService _productService;
        private IPictureService _pictureService;


        public ShoppingCartController(IShoppingCartService shoppingCartService, IWorkContext workContext, IProductService productService, IPictureService pictureService)
        {
            _shoppingCartService = shoppingCartService;
            _workContext = workContext;
            _productService = productService;
            _pictureService = pictureService;
        }

        // GET: ShoppingCart
        public ActionResult Index()
        {
            var customerId = _workContext.CurrentCustomer.Id;
            var cart = _shoppingCartService.GetShoppingCart(customerId);

            var model = PrepareShoppingCartModel(cart);

            return View("cart",model);
        }

        private ShoppingCartModel PrepareShoppingCartModel(IList<ShoppingCartItem> cart)
        {
            var model = new ShoppingCartModel();

            foreach (var shoppingCartItem in cart)
            {
                var pv = _productService.GetItemById(shoppingCartItem.ItemId);


                var shoppingCartItemModel = new ShoppingCartItemModel()
                {
                    AttributeInfo = shoppingCartItem.AttributesXml,
                    PictureUrl = _pictureService.GetPictureUrl(pv.ItemGroup.ItemGroupPictures[0].PictureId, 120, false),
                    ItemId = pv.Id,
                    ProductId = pv.Product.Id,
                    ProductName = string.IsNullOrEmpty(pv.Product.ProductName) ? pv.Product.ShortDescription : pv.Product.ProductName,
                    Quantity = shoppingCartItem.Quantity,
                  
                    SubTotal = pv.Price * shoppingCartItem.Quantity,
                    UnitPrice = pv.Price,
                };

                model.Items.Add(shoppingCartItemModel);

            }

            model.Total = model.Items.Sum(c => c.SubTotal);

            return model;
        }
    }

    public class ShoppingCartModel
    {
        public IList<ShoppingCartItemModel> Items { get; set; }
        public ShoppingCartModel()
        {
            Items = new List<ShoppingCartItemModel>();
        }
        public decimal Total { get; set; }
    }

    public partial class ShoppingCartItemModel
    {
        public ShoppingCartItemModel()
        {
            // Warnings = new List<string>();

        }
        public int Id { get; set; }

        public string PictureUrl { get; set; }

        public int ProductId { get; set; }
        public int ItemId { get; set; }

        public string ProductName { get; set; }

        public decimal UnitPrice { get; set; }
        public int Quantity { get; set; }

        public decimal SubTotal { get; set; }

        public string AttributeInfo { get; set; }


        // public IList<string> Warnings { get; set; }

    }
}